/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aternity.geolocations.draw;

import java.awt.BorderLayout;
import java.awt.geom.Point2D;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author danielo
 */
public class GeometryFromCsvFile extends javax.swing.JFrame {

    DrawingPanel drawingPanel = new DrawingPanel();

    /**
     * Creates new form DrawPolygonFrame
     */
    public GeometryFromCsvFile() {
        initComponents();
        this.drawingJP.add(BorderLayout.CENTER, drawingPanel);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        drawingJP = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        fileCombo = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jToolBar1 = new javax.swing.JToolBar();
        jMenuBar1 = new javax.swing.JMenuBar();
        insertMenu = new javax.swing.JMenu();
        insertPolygonMI = new javax.swing.JMenuItem();
        insertPointMI = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        autoScaleMI = new javax.swing.JMenuItem();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        drawingJP.setLayout(new java.awt.BorderLayout());

        jPanel2.setPreferredSize(new java.awt.Dimension(1138, 50));

        fileCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "state.geom.csv", "country.geom.csv" }));
        fileCombo.setToolTipText("");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(fileCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(142, 142, 142)
                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(446, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fileCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        drawingJP.add(jPanel2, java.awt.BorderLayout.PAGE_START);

        getContentPane().add(drawingJP, java.awt.BorderLayout.CENTER);

        jToolBar1.setRollover(true);
        getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_START);

        insertMenu.setText("Edit");

        insertPolygonMI.setText("Polygon");
        insertPolygonMI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertPolygonMIActionPerformed(evt);
            }
        });
        insertMenu.add(insertPolygonMI);

        insertPointMI.setText("Insert Point");
        insertPointMI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertPointMIActionPerformed(evt);
            }
        });
        insertMenu.add(insertPointMI);

        jMenuItem1.setText("Clear");
        insertMenu.add(jMenuItem1);

        jMenuBar1.add(insertMenu);

        jMenu2.setText("Transform");

        autoScaleMI.setText("Auto Scale");
        autoScaleMI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                autoScaleMIActionPerformed(evt);
            }
        });
        jMenu2.add(autoScaleMI);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void insertPolygonMIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertPolygonMIActionPerformed
        String polygonData = readPolygonFromUser();
        drawingPanel.setPolygonData(polygonData);
    }//GEN-LAST:event_insertPolygonMIActionPerformed

    private void autoScaleMIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_autoScaleMIActionPerformed
        drawingPanel.autoScaleAndTranslate();
    }//GEN-LAST:event_autoScaleMIActionPerformed

    private void insertPointMIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertPointMIActionPerformed
        String xy = JOptionPane.showInputDialog("Point: X,Y");
        final String[] splits = StringUtils.split(xy, ',');
        this.drawingPanel.addPoint(new Point2D.Double(Double.parseDouble(splits[0]), Double.parseDouble(splits[1])));
    }//GEN-LAST:event_insertPointMIActionPerformed

    void readPolygonDataFileIfExists() {
        File f = new File("polygon.data");
        if (!f.exists()) {
            return;
        }
        System.out.println("Reading polygon data from file.");
        try {
            String polygonData = FileUtils.readFileToString(f);
            System.out.println(polygonData);
            drawingPanel.setPolygonData(polygonData);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    String readPolygonFromUser() {
        JTextField textArea = new JTextField();
        textArea.setColumns(30);
        textArea.setSize(textArea.getPreferredSize().width, 1);
        JOptionPane.showMessageDialog(
                null, textArea, "Insert Polygon Data", JOptionPane.PLAIN_MESSAGE);
        return textArea.getText();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GeometryFromCsvFile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GeometryFromCsvFile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GeometryFromCsvFile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GeometryFromCsvFile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GeometryFromCsvFile f = new GeometryFromCsvFile();
                f.setSize(1000, 800);
                f.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem autoScaleMI;
    private javax.swing.JPanel drawingJP;
    private javax.swing.JComboBox fileCombo;
    private javax.swing.JMenu insertMenu;
    private javax.swing.JMenuItem insertPointMI;
    private javax.swing.JMenuItem insertPolygonMI;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables
}
